/**
 * 宽带预约列表
 */
(function($) {
  var filterType = 3;
  var pageNumber = 0;
  var conType = 0;
  var conText = "";
  var startDate = "";
  var endDate = "";
  var empCode = "";

  var bulletinList = new Array();
  /*
  封装参数
  */
  var getMyParams = function(id, title, content) {
    var params = "?";
    var myParam = getParams();
    if (myParam) {
      params += myParam;
      if (id || title || content) {
        params += "&";
      }
    }
    if (id) {
      params += "id=" + id;
    }
    if (title) {
      params += "&title=" + encodeURIComponent(title);
    }
    if (content) {
      params += "&content=" + encodeURIComponent(content);
    }

    return params;
  };

  var bind = function() {
    itemOnClick("#bulletin-list > li");
  }

  $("#submit-btn").on("click", function() {
    var type = $("#type").val();
    if (type) {
      conType = type;
    }
    var key = $("#key").val();
    if (key) {
      conText = key;
    }
    var start = $("#start").val();
    if (start) {
      startDate = start;
    }
    var end = $("#end").val();
    if (end) {
      endDate = end;
    }
    $("#search").addClass('am-hide');
  });

  var itemOnClick = function(object) {
    $(object).off("click"); //取消li事件
    /*li事件*/
    $(object).on("click", function() {
      var $siblingsNode = $(this).siblings();
      var primaryClass = "select-item";
      var defaultColor = "#fff";
      var selectColor = "#0c79b1";
      var idVal = $(this).find("input[name='id']").val();
      var titleVal = $(this).find("div[name='title']").html();
      var content = $(this).find("input[name='content']").val();
      var createDate = $(this).find("div[name='create_date']").html();
      var creator = $(this).find("input[name='creator']").val();

      if ($siblingsNode.hasClass(primaryClass)) {
        $siblingsNode.removeClass(primaryClass);
        $siblingsNode.css("background-color", defaultColor);
      }
      if ($(this).hasClass(primaryClass)) {
        $(this).removeClass(primaryClass);
        $(this).css("background-color", defaultColor);
        $id.val("");
        $name.html("");
      } else {
        $(this).addClass(primaryClass);
        $(this).css("background-color", selectColor);
      }
      setCookie("bulletin_title", titleVal, 1);
      setCookie("bulletin_content", content, 1);
      setCookie("bulletin_date", createDate, 1);
      setCookie("bulletin_creator", creator, 1);
      window.location.href = "bulletin_detail.html" + getMyParams(idVal);
      event.stopPropagation();
    });
  }

  var showBulletinList = function(list) {
    var dataList = list;

    var $bulletin_list = $('#amz-list');
    var source = $bulletin_list.text();
    var template = Handlebars.compile(source);
    var data = {
      bulletinList: dataList,
      "error": "无公告信息!"
    };
    $("#bulletin-list").children('li').remove();
    $("#bulletin-list").children('div').remove();
    var html = template(data);
    $bulletin_list.before(html);
    bind();
  }

  /*
  显示数据
  */
  var showDatas = function(data) {
    var $bulletin_list = $('#amz-list');
    var source = $bulletin_list.text();
    var template = Handlebars.compile(source);
    var unreadCount = 0;
    var readCount = 0;

    if (data.rtnCode != 1000) {
      alert(data.rtnMsg);
      return;
    }
    var list = data.return.notifyInfoList;

    if (list.length == 0) {
      pageNumber--;
      console.log("pageNumber=" + pageNumber);
    }

    console.log("filterType=" + filterType);

    for (var i = 0; i < list.length; i++) {
      list[i].createDate = new Date(list[i].createDate).format("yyyy-MM-dd");
      if (list[i].notifyContent) {
        list[i].notifyContent = list[i].notifyContent.replace(/\r\n/ig, "<br/>");
      }
      bulletinList.push(list[i]);
    }
    showBulletinList(bulletinList);
  };

  /*
  加载数据
  */
  var loadDatas = function(startDate, endDate, conType, conText, pageNumber) {
    console.log("startDate=" + startDate + ",endDate=" + endDate + ",conType=" + conType + ",conText=" + conText + ",pageNumber=" + pageNumber);
    showLoadingBar($("#loadingbar"), true);
    var empCode = getParamValue("empCode");
    var data = JSON.stringify(getBulletinQueryJsonParams(getParamValue("token"), empCode, startDate, endDate, conType, conText, pageNumber, 30));
    console.log("data=" + data);
    $.ajax({
      type: "post",
      contentType: "application/json",
      url: Constants.rpcEsopHost + "queryNotifyInfos",
      data: data,
      dataType: "json",
      headers: {
      'token': getParamValue('token'),
      'saleType': getParamValue('saleType')
    },
    success: function(data) {
      if (data.ResultCode === 1009) {
        alert('登陆超时，请重新登录！');
        jumpToSignin();
        return;
      }
        showLoadingBar($("#loadingbar"), false);
        pageData = data;
        console.info(data);
        showDatas(data);
      },
      error: function(textStatus) {
        showLoadingBar($("#loadingbar"), false);
        alert("获取系统公告失败！");
        console.info("request error");
      }
    });
  };

  var search = function() {
    var type = getParamValue("conType");
    if (type) {
      conType = type;
    }
    var key = getParamValue("conText");
    if (key) {
      conText = decodeURIComponent(key);
    }
    var start = getParamValue("startDate");
    if (start) {
      startDate = start + " 00:00:00";
    }
    var end = getParamValue("endDate");
    if (end) {
      endDate = end + " 23:59:59";
    }
    loadDatas(startDate, endDate, conType, conText, pageNumber);
  }

  $(function() {
    /*滚动条*/
    var $doc = $(document);
    $doc.scroll(function() {
      console.log("height = " + ($doc.height() - $(window).height()));
      console.log("scrollTop = " + Math.ceil($doc.scrollTop()));
      console.log("Math.ceil($doc.scrollTop()) = " + Math.ceil($doc.scrollTop()));
      if (Math.ceil($doc.scrollTop()) >= ($doc.height() - $(window).height())) {
        var $msg = $("#no-data-msg");
        console.log("debug 1");
        if (!$.isEmptyObject(pageData)) {
          console.log("debug 2");
          if (!$msg.hasClass("am-hide")) {
            console.log("debug 3");
            $msg.addClass("am-hide");
          }
          console.log("debug 4");
          loadDatas(startDate, endDate, conType, conText, pageNumber + 1);
          pageNumber += 1;
        } else {
          console.log("debug 5");
          if ($msg.hasClass("am-hide")) {
            console.log("debug 6");
            $msg.removeClass("am-hide");
          }
        }
      }
    });   
    search();
  });
})(jQuery);